import Mat4 from "./Mat4";
import Quaternion from "./Quaternion";
import Renderer from "./Renderer";
import Trs from "./Trs";
import Vec3 from "./Vec3";


export default class Camera {
    position = null;
    rotation = null;
    viewMatrix = null;
    constructor() {
        this.position = new Vec3(0, 0, -5);
        this.rotation = new Quaternion();
        this.viewMatrix = new Mat4();
        this.updateViewMatrix()
    }
    render() {
        // TODO: 实现相机渲染
        this.updateViewMatrix()
    }
    updateViewMatrix() {
        // viewMatrix[12] = -this.position.x;
        // viewMatrix[13] = -this.position.y;
        // viewMatrix[14] = this.position.z;
        // console.log(cameraX,cameraY,cameraZ)
        // viewMatrix = multiplyMatrix(viewMatrix,newViewMatrix)
        // viewMatrix = new Float32Array(newViewMatrix)
        // console.log('updateViewMatrix', viewMatrix)
        // this.viewMatrix = { matrix: this.rotation.toRotationMatrix() };
        // this.viewMatrix.matrix[14] = this.position.z;
        let trs = [
            -this.position.x,
            -this.position.y,
            this.position.z,
            this.rotation.x,
            this.rotation.y,
            this.rotation.z,
            this.rotation.w,
            1,
            1,
            1,
        ]
        Trs.toMat4(this.viewMatrix, trs)
        // console.log('camera updateViewMatrix', this.viewMatrix)
    }
    // 设置投影矩阵（透视投影）
    setPerspectiveProjectionMatrix() {
        let canvas = Renderer.instance.canvas;
        const fovy = Math.PI / 3; // 45度
        const tanHalfFovy = Math.tan(fovy / 2);
        const aspect = canvas.width / canvas.height;
        const near = 0.51;
        const far = 1000.0;

        const projectionMatrix = new Float32Array(16);
        projectionMatrix[0] = 1.0 / (aspect * tanHalfFovy);
        projectionMatrix[5] = 1.0 / tanHalfFovy;
        projectionMatrix[10] = -(far + near) / (far - near);
        projectionMatrix[11] = -1.0;
        projectionMatrix[14] = -(2.0 * far * near) / (far - near);
        projectionMatrix[15] = 0.0;
        return projectionMatrix;
    }
    // 设置投影矩阵（正交投影）
    setOrthographicProjectionMatrix() {
        let canvas = Renderer.instance.canvas;
        const left = -canvas.width / 2;
        const right = canvas.width / 2;
        const bottom = -canvas.height / 2;
        const top = canvas.height / 2;
        const near = 0.1;
        const far = 100000.0;
        const projectionMatrix = new Float32Array(16);
        projectionMatrix[0] = 2.0 / (right - left);
        projectionMatrix[5] = 2.0 / (top - bottom);
        projectionMatrix[10] = -2.0 / (far - near);
        projectionMatrix[12] = -(right + left) / (right - left);
        projectionMatrix[13] = -(top + bottom) / (top - bottom);
        projectionMatrix[14] = -(far + near) / (far - near);
        projectionMatrix[15] = 1.0;
        return projectionMatrix;
    }
    // 实现 lookAt 函数
    lookAt(target: Vec3, up: Vec3 = new Vec3(0, 1, 0)): Quaternion {
        let eye = this.position;
        // 计算方向向量
        const forward = Vec3.subtract(target, eye).normalize();
        const right = Vec3.cross(up, forward).normalize();
        const upDir = Vec3.cross(forward, right);

        // 计算旋转轴和角度
        const axis = Vec3.cross(new Vec3(0, 0, 1), forward);
        const angle = Math.acos(Vec3.dot(new Vec3(0, 0, 1), forward));

        // 构造四元数
        const forwardQuat = Quaternion.fromAxisAngle(axis, angle);

        // 计算围绕y轴的旋转
        const yAxis = Vec3.cross(new Vec3(0, 1, 0), upDir);
        const yAngle = Math.acos(Vec3.dot(new Vec3(0, 1, 0), upDir));
        const upQuat = Quaternion.fromAxisAngle(yAxis, yAngle);
        console.log('upQuat',upQuat)
        // 合并旋转
        upQuat.multiply(forwardQuat).normalize();
        this.rotation = upQuat;
        return upQuat
    }
}